; Inno Setup install script for mediate
; (C) Copyright 2004-2009 @home media limited

; Configuration settings to determine type of setup
#define MyAppName "mediate"
#define MyOutputBaseFilename "mediateSetup"
#define MyWizardImageFile "gfx\wizard-mediate.bmp"

#define MyAppId "{{36F1FDBA-6C51-472D-99A0-616AE8720DEE}"
#define MyAppVersion GetFileVersion("staging\bin\mediate.exe")
#define MyAppPublisher "@home media limited"
#define MyAppCopyright "Copyright @home media limited 2004-2009"
#define MyAppURL "http://www.athomemedia.co.uk"
#define MyAppSupportURL "http://www.athomemedia.co.uk/support"


[Setup]
AppId={#MyAppId}
AppName={#MyAppName}
AppVersion={#MyAppVersion}
AppVerName={#MyAppName} {#MyAppVersion}
AppPublisher={#MyAppPublisher}
AppCopyright={#MyAppCopyright}
AppPublisherURL={#MyAppURL}
AppSupportURL={#MyAppSupportURL}
AppUpdatesURL={#MyAppURL}
AppMutex=@home media limited - mediate
AppReadmeFile={app}\docs\ReadMe.html
DefaultDirName={pf}\@home media
DefaultGroupName=@home media
AllowNoIcons=no
SourceDir=staging
LicenseFile=docs\license.txt
OutputBaseFilename={#MyOutputBaseFilename}
ChangesAssociations=yes
Compression=lzma
SolidCompression=yes
WizardImageFile={#MyWizardImageFile}
WizardSmallImageFile=gfx\wizard-small.bmp
SetupIconFile=gfx\mediate.ico
PrivilegesRequired=admin
UninstallDisplayIcon={app}\bin\mediate.exe
VersionInfoDescription=mediate setup
VersionInfoVersion={#MyAppVersion}


[Languages]
Name: english; MessagesFile: compiler:Default.isl


[Tasks]
Name: desktopicon; Description: {cm:CreateDesktopIcon}; GroupDescription: {cm:AdditionalIcons}; Flags: unchecked
Name: quicklaunchicon; Description: {cm:CreateQuickLaunchIcon}; GroupDescription: {cm:AdditionalIcons}; Flags: unchecked
Name: autolaunchmediate; Description: {cm:AutoLaunchMediate}; GroupDescription: {cm:AutoLaunch}
Name: autolaunchhelper; Description: {cm:AutoLaunchHelper}; GroupDescription: {cm:AutoLaunch}


[Files]
; Install the main mediate binaries
Source: bin\mediate.exe; DestDir: {app}\bin; Flags: ignoreversion restartreplace
Source: bin\mediatedesigner.exe; DestDir: {app}\bin; Flags: ignoreversion restartreplace
Source: bin\mmm.exe; DestDir: {app}\bin; Flags: ignoreversion restartreplace
Source: bin\mediateaudio.dll; DestDir: {app}\bin; Flags: ignoreversion regserver restartreplace
Source: bin\mediatevis.dll; DestDir: {app}\bin; Flags: ignoreversion regserver restartreplace
Source: bin\sqlite3.dll; DestDir: {app}\bin; Flags: ignoreversion restartreplace
Source: bin\d3dx9_25.dll; DestDir: {app}\bin; Flags: ignoreversion restartreplace
Source: bin\DXErr9ab.dll; DestDir: {app}\bin; Flags: ignoreversion restartreplace
Source: bin\mediatehelper.exe; DestDir: {app}\bin; BeforeInstall: ShutdownHelper; Flags: ignoreversion restartreplace
Source: bin\msvcahm.dll; DestDir: {app}\bin; Flags: ignoreversion restartreplace
Source: bin\Streamzap.dll; DestDir: {app}\bin; Flags: ignoreversion restartreplace

; Install plugins and dependencies if present in staging folder
Source: bin\mediatenavigation.dll; DestDir: {app}\bin; Flags: ignoreversion skipifsourcedoesntexist restartreplace
Source: bin\mediatevelleman.dll; DestDir: {app}\bin; Flags: ignoreversion skipifsourcedoesntexist restartreplace
Source: bin\mediateweather.dll; DestDir: {app}\bin; Flags: ignoreversion skipifsourcedoesntexist restartreplace
Source: bin\K8055D.dll; DestDir: {app}\bin; Flags: ignoreversion skipifsourcedoesntexist restartreplace
Source: bin\USBRadio.dll; DestDir: {app}\bin; Flags: ignoreversion skipifsourcedoesntexist restartreplace

; Install documentation
Source: docs\readme.html; DestDir: {app}\docs; Flags: ignoreversion isreadme restartreplace
Source: docs\copyright.txt; DestDir: {app}\docs; Flags: ignoreversion restartreplace
Source: docs\UserGuide.pdf; DestDir: {app}\docs; Flags: ignoreversion restartreplace

; Copy shortcut icons and other resource files
Source: gfx\*.ico; DestDir: {app}\resources; Flags: ignoreversion restartreplace
Source: resources\*; DestDir: {app}\resources; Flags: ignoreversion restartreplace

; Install the remaining files
Source: conf\*; DestDir: {app}\conf; Flags: ignoreversion restartreplace
Source: plugins\mediateweather.xml; DestDir: {app}\plugins; Flags: ignoreversion restartreplace
Source: themes\tv\*; DestDir: {app}\themes\tv; Flags: ignoreversion recursesubdirs restartreplace


[Dirs]
; Create an empty license directory: may not be needed yet, but keep for future use
Name: {app}\license
Name: {app}\thumbnails
Name: {app}\logs


[InstallDelete]
; Delete icons (including legacy ones) so they are explicitly recreated by install
Name: {group}\mediate media manager.lnk; Type: files
Name: {group}\www.athomemedia.co.uk.lnk; Type: files
Name: {group}\mediate support.lnk; Type: files
Name: {group}\mediate media manager.lnk; Type: files
Name: {group}\User Guide.lnk; Type: files
Name: {group}\Read Me.lnk; Type: files
Name: {group}\mediate helper.lnk; Type: files
Name: {group}\mediate.lnk; Type: files
Name: {group}\Configure mediate.lnk; Type: files
Name: {commondesktop}\mediate.lnk; Type: files


[Icons]
; Use explicit icons for shortcuts - this seems more reliable than pointing to executable
Name: {group}\{cm:ConfigureMediate}; Filename: {app}\bin\mediate.exe; Parameters: -configure; IconFilename: {app}\resources\mediate.ico
Name: {group}\{cm:Mediate}; Filename: {app}\bin\mediate.exe; IconFilename: {app}\resources\mediate.ico
Name: {group}\{cm:MediateDesigner}; Filename: {app}\bin\mediatedesigner.exe; IconFilename: {app}\resources\mediate.ico
Name: {group}\{cm:MediateHelper}; Filename: {app}\bin\mediatehelper.exe; IconFilename: {app}\resources\remote.ico; Flags: createonlyiffileexists
Name: {group}\{cm:MediateMediaManager}; Filename: {app}\bin\mmm.exe; IconFilename: {app}\resources\mediate.ico
Name: {group}\{cm:ReadMe}; Filename: {app}\docs\readme.html
Name: {group}\{cm:UninstallProgram,{#MyAppName}}; Filename: {uninstallexe}
Name: {group}\{cm:UserGuide}; Filename: {app}\docs\userguide.pdf
Name: {group}\{cm:ProgramOnTheWeb}; Filename: {#MyAppURL}

Name: {commondesktop}\{cm:Mediate}; Filename: {app}\bin\mediate.exe; IconFilename: {app}\resources\mediate.ico; Tasks: desktopicon
Name: {userappdata}\Microsoft\Internet Explorer\Quick Launch\{cm:Mediate}; Filename: {app}\bin\mediate.exe; IconFilename: {app}\resources\mediate.ico; Tasks: quicklaunchicon


[Registry]
; Remove legacy uninstall key from NSIS installer
Root: HKLM; SubKey: SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\@home media - mediate; Flags: deletekey dontcreatekey

; Remove autostart keys for helper and mediate, then re-add them if requested during install
Root: HKLM; SubKey: SOFTWARE\Microsoft\Windows\CurrentVersion\Run; ValueType: string; ValueName: {cm:mediatehelper}; Flags: deletevalue
Root: HKLM; SubKey: SOFTWARE\Microsoft\Windows\CurrentVersion\Run; ValueType: string; ValueName: {cm:mediate}; Flags: deletevalue
Root: HKLM; SubKey: SOFTWARE\Microsoft\Windows\CurrentVersion\Run; ValueType: string; ValueName: {cm:mediatehelper}; ValueData: """{app}\bin\mediatehelper.exe"""; Flags: uninsdeletevalue; Tasks: autolaunchhelper
Root: HKLM; SubKey: SOFTWARE\Microsoft\Windows\CurrentVersion\Run; ValueType: string; ValueName: {cm:mediate}; ValueData: """{app}\bin\mediate.exe"""; Flags: uninsdeletevalue; Tasks: autolaunchmediate

; Create keys for application paths so executables can be launched with Start / Run
Root: HKLM; Subkey: SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\mediate.exe; ValueType: string; ValueData: {app}\bin\mediate.exe; Flags: uninsdeletekey
Root: HKLM; Subkey: SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\mediatedesigner.exe; ValueType: string; ValueData: {app}\bin\mediatedesigner.exe; Flags: uninsdeletekey
Root: HKLM; Subkey: SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\mmm.exe; ValueType: string; ValueData: {app}\bin\mmm.exe; Flags: uninsdeletekey
Root: HKLM; Subkey: SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\mediatehelper.exe; ValueType: string; ValueData: {app}\bin\mediatehelper.exe; Flags: uninsdeletekey

; Create keys to associate .mediateTheme with mediate executable
Root: HKCR; Subkey: .mediateTheme; ValueType: string; ValueName: ; ValueData: mediateThemeFile; Flags: uninsdeletevalue
Root: HKCR; Subkey: mediateThemeFile; ValueType: string; ValueName: ; ValueData: mediate Theme Archive; Flags: uninsdeletekey
Root: HKCR; Subkey: mediateThemeFile\DefaultIcon; ValueType: string; ValueName: ; ValueData: {app}\bin\mediate.exe,0
Root: HKCR; Subkey: mediateThemeFile\Shell\Open\Command; ValueType: string; ValueName: ; ValueData: "{app}\bin\mediate.exe ""%1"""


[UninstallRun]
; Shutdown the mediate helper from the systray - won't matter if it's not running
Filename: {app}\mediatehelper.exe; Parameters: SHUTDOWN; Flags: waituntilidle skipifdoesntexist


[UninstallDelete]
; Clean up thumbnail and log files created by mediate
Name: {app}\thumbnails; Type: filesandordirs
Name: {app}\logs; Type: filesandordirs


[Messages]
;Customise messages for a few pages to better match original NSIS installer
WelcomeLabel1=Welcome to the [name/ver] setup wizard
WelcomeLabel2=This wizard will install [name/ver] on your computer.%n%nIt is recommended that you close all other applications before continuing. This will make it possible to update relevant system files without having to reboot your computer.
LicenseLabel=Please review the license terms before installing.
FinishedHeadingLabel=Completing the [name/ver] setup wizard
FinishedLabel=Setup has finished installing [name] on your computer.


[CustomMessages]
ConfigureMediate=Configure mediate
Mediate=mediate
MediateDesigner=mediate designer
MediateHelper=mediate helper
MediateMediaManager=mediate media manager
ReadMe=Read Me
UserGuide=User Guide
CreateDesktopIcon=Create a &Desktop icon
ProgramOnTheWeb=Visit @home media site
AutoLaunch=Automatic start:
AutoLaunchMediate=Automatically run &mediate when Windows starts
AutoLaunchHelper=Automatically run mediate &helper when Windows starts


[Code]
var
  DirectXInstalled: Boolean;

// Break a period delimited version string into an array of 4 integers
procedure DecodeVersion( verstr: String; var verint: array of Integer );
var
  i,p: Integer; s: string;
begin
  // initialize array
  verint := [0,0,0,0];
  i := 0;
  while ( (Length(verstr) > 0) and (i < 4) ) do
  begin
  	p := pos('.', verstr);
  	if p > 0 then
  	begin
      if p = 1 then s:= '0' else s:= Copy( verstr, 1, p - 1 );
  	  verint[i] := StrToInt(s);
  	  i := i + 1;
  	  verstr := Copy( verstr, p+1, Length(verstr));
  	end
  	else
  	begin
  	  verint[i] := StrToInt( verstr );
  	  verstr := '';
  	end;
  end;

end;

// This function compares version string
// return -1 if ver1 < ver2
// return  0 if ver1 = ver2
// return  1 if ver1 > ver2
function CompareVersion( ver1, ver2: String ) : Integer;
var
  verint1, verint2: array of Integer;
  i: integer;
begin
  SetArrayLength( verint1, 4 );
  DecodeVersion( ver1, verint1 );

  SetArrayLength( verint2, 4 );
  DecodeVersion( ver2, verint2 );

  Result := 0; i := 0;
  while ( (Result = 0) and ( i < 4 ) ) do
  begin
  	if verint1[i] > verint2[i] then
  	  Result := 1
  	else
      if verint1[i] < verint2[i] then
  	    Result := -1
  	  else
  	    Result := 0;

  	i := i + 1;
  end;

end;

// DirectX version is stored in registry as 4.majorversion.minorversion
// DirectX 8.0 is 4.8.0
// DirectX 8.1 is 4.8.1
// DirectX 9.0 is 4.9.0
function GetDirectXVersion(): String;
var
  sVersion:  String;
begin
  sVersion := '';

  RegQueryStringValue( HKLM, 'SOFTWARE\Microsoft\DirectX', 'Version', sVersion );

  Result := sVersion;
end;

// Customise setup initialisation to check for DirectX 9 or higher
function InitializeSetup(): Boolean;
begin
  // Set global to indicate whether DirectX is installed
  DirectXInstalled := CompareVersion( GetDirectXVersion(), '4.9.0') > -1;

  // Prompt user to install DirectX if not found
  if not DirectXInstalled then
  begin
    MsgBox('DirectX does not appear to be installed. mediate requires DirectX version 9.0 or higher ' +
           'to run. Please remember to update DirectX after installing mediate.' + #13#10#13#10 +
           'DirectX may be downloaded from:' + #13#10 +
           'http://www.microsoft.com/windows/directx/default.mspx',
           mbInformation, MB_OK);
  end;

  // Continue installation
  Result := True;
end;

// Shutdown mediate helper before installing to avoid having to restart windows
procedure ShutdownHelper();
var
  HelperExe: String;
  ResultCode: Integer;
begin
  // Expand executable filename for helper
  HelperExe := ExpandConstant( CurrentFileName());

  // If helper found then instruct it to shutdown
  if FileExists(HelperExe) then
  begin
    Exec(HelperExe, 'SHUTDOWN', '', SW_SHOW, ewWaitUntilTerminated, ResultCode);
  end;
end;
